Census Data

Community
analysis
Rstudio
Published

February 28, 2022

Show the code
# Set a year first
this.year = 2021

### BASE PLOT EXAMPLE
### Washington
or_tracts <- tracts(state = 'OR', 
                    cb = T, year = this.year)

# This is the structure of spatial data 
head(or_tracts)
Simple feature collection with 6 features and 13 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -122.9903 ymin: 44.88572 xmax: -118.7871 ymax: 45.7816
Geodetic CRS:  NAD83
  STATEFP COUNTYFP TRACTCE             AFFGEOID       GEOID   NAME
1      41      047  001607 1400000US41047001607 41047001607  16.07
2      41      005  980000 1400000US41005980000 41005980000   9800
3      41      051  001301 1400000US41051001301 41051001301  13.01
4      41      067  031516 1400000US41067031516 41067031516 315.16
5      41      059  950500 1400000US41059950500 41059950500   9505
6      41      051  005103 1400000US41051005103 41051005103  51.03
             NAMELSAD STUSPS        NAMELSADCO STATE_NAME LSAD      ALAND
1  Census Tract 16.07     OR     Marion County     Oregon   CT    1814790
2   Census Tract 9800     OR  Clackamas County     Oregon   CT 1634590508
3  Census Tract 13.01     OR  Multnomah County     Oregon   CT     744911
4 Census Tract 315.16     OR Washington County     Oregon   CT    1879977
5   Census Tract 9505     OR   Umatilla County     Oregon   CT 1073211610
6  Census Tract 51.03     OR  Multnomah County     Oregon   CT     433965
   AWATER                       geometry
1       0 MULTIPOLYGON (((-122.9903 4...
2 6684470 MULTIPOLYGON (((-122.266 45...
3       0 MULTIPOLYGON (((-122.6348 4...
4       0 MULTIPOLYGON (((-122.8068 4...
5       0 MULTIPOLYGON (((-119.4346 4...
6  155315 MULTIPOLYGON (((-122.6776 4...
Show the code
plot(or_tracts)

Show the code
# GGPLOT
ggplot(or_tracts) + 
  geom_sf() + 
  coord_sf()

Show the code
### GET CENSUS DATA
### B25077_001E: MEDIAN HOME VALUE
or <- get_acs(geography = "tract", year=this.year,
              state = "OR", 
              variables = "B25077_001E")%>%
  mutate(GEO_ID=paste0("1400000US", GEOID))

head(or)
# A tibble: 6 × 6
  GEOID       NAME                                  varia…¹ estim…²   moe GEO_ID
  <chr>       <chr>                                 <chr>     <dbl> <dbl> <chr> 
1 41001950100 Census Tract 9501, Baker County, Ore… B25077…  265000 46736 14000…
2 41001950200 Census Tract 9502, Baker County, Ore… B25077…  175200 40020 14000…
3 41001950300 Census Tract 9503, Baker County, Ore… B25077…  140600 22155 14000…
4 41001950400 Census Tract 9504, Baker County, Ore… B25077…  190500 36472 14000…
5 41001950500 Census Tract 9505, Baker County, Ore… B25077…  191100 11383 14000…
6 41001950600 Census Tract 9506, Baker County, Ore… B25077…  237500 46978 14000…
# … with abbreviated variable names ¹​variable, ²​estimate
Show the code
joinrace<- geo_join(or_tracts, orPct, 
                 by_sp="GEOID", by_df="GEOID")

## SET GEOMETRY
oregon <- get_acs(geography = "tract", year=this.year,
               state = "OR",
               variables = "B25077_001E",
               geometry = TRUE)

#Plotting with MAPVIEW 
mapview(oregon, zcol = "estimate", legend = TRUE, 
        lwd=.25)
Show the code
## USE GEO_JOIN TO COMBINE SPATIAL DATA AND OTHER DATA FRAMES

joinOR<- geo_join(or_tracts, or, 
                 by_sp="GEOID", by_df="GEOID")

## USE TMAP PACKAGE
tm_shape(joinOR)+
  tm_fill("estimate", style = "quantile", n=7, palette = "Greens")+
  tm_legend(bg.color="white", bg.alpha=0.6)+
  tm_style("gray")

Show the code
## SET GEOMETRY
oregon <- get_acs(geography = "tract", year=this.year,
               state = "OR",
               variables = "B25077_001E",
               geometry = TRUE)

#Plotting with MAPVIEW 
mapview(oregon, zcol = "estimate", legend = TRUE, 
        lwd=.25)
Show the code
## Plotting with LEAFLET

## Leaflet with reactive
pal<-colorNumeric("Greens", domain=0:ceiling(max(oregon$estimate, na.rm=TRUE)))
popup<-paste("Tract: ", as.character(substring(oregon$GEOID, 6, 11)), "<br>",
             "Median Home Value: ", as.character(oregon$estimate))
leaflet()%>%
  addProviderTiles("CartoDB.Positron")%>%
  addPolygons(data=oregon,
              fillColor= ~pal(oregon$estimate),
              fillOpacity = 0.7,
              weight = 0.4,
              smoothFactor = 0.2,
              popup = popup)
Show the code
## Total Hispanic/Latino
or <- get_acs(geography = "county", variables = "B03001_001", state = 'OR', year = 2021)

or %>%
 ggplot(aes(x = estimate, y = reorder(NAME, estimate))) +
 geom_point(color = "red", size = 1) +
  labs(title = "Latino Population by county in Oregon",
       subtitle = "2018-2021 American Community Survey",
       y = "",
       x = "ACS estimate")

Show the code
oregon <- get_acs(geography = "tract", year=2021,
               state = "OR",
               variables = "B03001_001",
               geometry = TRUE)

#Plotting with MAPVIEW 
mapview(oregon, zcol = "estimate", legend = TRUE, 
        lwd=.25)